//
//  YZMGestureViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/4/20.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMGestureViewController.h"
#import "YZMAlertView.h"
#import "YZMSeparatorCell.h"
#import "YZMLockPassword.h"
#import "YZMLockView.h"

@interface YZMGestureViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation YZMGestureViewController{
    
    UITableView *_tableView;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)init {
    
    if (self = [super init]) {
        self.title = @"手势密码";
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    }
    
    return self;
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.edges.equalTo(self.view);
        maker.left.right.bottom.equalTo(self.view);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:_tableView selector:@selector(reloadData) name:YZMUserDetailUpdateNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [_tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"GestureLockCell";
    YZMSeparatorCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[YZMSeparatorCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.textLabel.textColor = [YZMAppStyle colorForWhite];
        cell.textLabel.font = [YZMAppStyle largeSizeFont];
    }
    
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row;
    if ([YZMLockPassword loadLockPassword]) {
        cell.leftMargin = 15.0;
        cell.rightMargin = 0.0;
        indexCount.count = 2;
    } else  {
        cell.leftMargin = 0.0;
        cell.rightMargin = 0.0;
        indexCount.count = 1;
    }
    cell.indexCount = indexCount;
    
    switch (indexPath.row) {
        case 0:{
            cell.textLabel.text = @"手势密码";
            cell.textLabel.font = [YZMAppStyle mediumSizeFont];
            UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
            switchview.onTintColor = [YZMAppStyle colorForIconColor];
            [switchview addTarget: self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:CGRectMake(0, 0, switchview.frame.size.width, switchview.frame.size.height)];
            [button setBackgroundColor:[UIColor clearColor]];
            [button addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventTouchUpInside];
            
            [switchview addSubview:button];
            
            cell.accessoryView = switchview;
            
            if ([YZMLockPassword loadLockPassword]) {
                
                switchview.on = YES;
            }else{
                
                switchview.on = NO;
            }
        }
            break;
        case 1:{
            
            cell.textLabel.text = @"修改手势密码";
            cell.textLabel.font = [YZMAppStyle mediumSizeFont];
            UIImageView *rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_red_arrow_right"]];
            cell.accessoryView = rightArrow;

            break;
        }
        default:
            break;
    }
    
    return cell;
}

- (void)switchValueChanged:(id)sender{
    
    if ([YZMLockPassword loadLockPassword]) {
        
        /*清除密码，提示是否确定*/
        YZMAlertView *alertView = [YZMAlertView new];
        __weak YZMAlertView *_alertView = alertView;
        alertView.titleLabel.text = @"关闭手势密码";
        alertView.contentLabel.text = @"关闭后无需手势解密就能查看您的账户，可能会有一定的安全风险，确定要关闭手势密码吗?";
        [alertView showInView:self.navigationController.view];
        alertView.confirmHandle = ^()
        {
            [_alertView dismissWithAnimationFlag:NO];
            [self showGestureSettingPageWithType:YZM_LOCK_TYPE_FOR_CLEAN];
            
            return YES;
        };
        [alertView show];
        
    }else{
        
        [self showGestureSettingPageWithType:YZM_LOCK_TYPE_FOR_CREATE];
    }
    
}

- (void)showGestureSettingPageWithType:(YZM_LOCK_TYPE)type{
    
    [YZMHCMInstance pushViewControllerWithName:@"lockView" andParams:@{@"type":@(type)}];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([YZMLockPassword loadLockPassword]) {
        
        return 2;
    }
    
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.row == 0) {
        
        [self switchValueChanged:nil];
        
    }else if (indexPath.row == 1) {
        
        [self showGestureSettingPageWithType:YZM_LOCK_TYPE_FOR_MODIFY];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 54;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 54;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionView = [[UIView alloc] init];
    [sectionView setBackgroundColor:[UIColor clearColor]];
    
    UILabel *label = [YZMAppStyle labelWithMediumLightGray];
    label.text = @"提高账户安全，开启手势密码";
    [sectionView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sectionView);
        make.left.equalTo(sectionView).with.offset(15);
    }];
    
    return sectionView;
}

@end
